﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace TaskProgramming
{
    class _005_Continuation
    {
        public static void Test()
        {
            var task = Task.Factory.StartNew(() =>
            {
                Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(1000);

                return "段光伟";
            });

            var t1 = task.ContinueWith(x =>
            {
                Console.WriteLine("Default:" + x.Result + "   ThreadId:" + Thread.CurrentThread.ManagedThreadId);
            });

            var t2 = task.ContinueWith(x =>
            {
                Console.WriteLine("OnlyOnCanceled:" + x.Result + "   ThreadId:" + Thread.CurrentThread.ManagedThreadId);
            }, TaskContinuationOptions.OnlyOnCanceled);

            var t3 = task.ContinueWith(x =>
            {
                Console.WriteLine("OnlyOnFaulted:" + x.Result + "   ThreadId:" + Thread.CurrentThread.ManagedThreadId);
            }, TaskContinuationOptions.OnlyOnFaulted);

            var t4 = task.ContinueWith(x =>
            {
                Console.WriteLine("OnlyOnRanToCompletion:" + x.Result + "   ThreadId:" + Thread.CurrentThread.ManagedThreadId);
            }, TaskContinuationOptions.OnlyOnRanToCompletion);

            Console.ReadLine();

            Console.WriteLine("t2.IsCanceled:" + t2.IsCanceled);
            Console.WriteLine("t2.IsCompleted:" + t2.IsCompleted);
        }
    }
}
